package com.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Link;
import com.service.LinkService;

@Controller @RequestMapping("/link")
public class LinkController {
	
	@Resource
	LinkService linkService;
	
	@RequestMapping("")
	public String link(ModelMap m){		
		List<Link> linkList=linkService.listLink();	
		m.put("linkList", linkList);		
		return "Link/link-list";	
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage(ModelMap m) {
		List<Link> linkList=linkService.listLink();
		m.put("linkList",linkList);
		return "Link/link-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Link link)throws IOException {
		link.setLinkStatus(1);
		link.setLinkCreateTime(new Date());
		link.setLinkUpdateTime(new Date());
		linkService.addLink(link);
		return "forward:/link";
	}
	
	@RequestMapping(value="/delete/{id}")
    public String deleteLink(@PathVariable("id") Integer id)  {
        linkService.deleteLink(id);
        return "forward:/link";
    }
	
	@RequestMapping(value="/edit/{id}")
    public String editLinkView(@PathVariable("id") Integer id,ModelMap m)  {
  
        Link linkCustom = linkService.getLinkById(id);
        m.put("linkCustom",linkCustom);
        
        List<Link> linkList = linkService.listLink(); 
        m.put("linkList",linkList);
        return "Link/edit";
    }
	
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editLinkSubmit(Link link) throws IOException {
        link.setLinkUpdateTime(new Date());
        linkService.updateLink(link);
        return "forward:/link";
    }
}